#DevBlog#

İmkanın sınırını zorlamadan imkansızı bilemezsin...

Belki bir gün, Başka bir evrende ya da ileride...
image

Unity Generate Terrain

Merhaba bu gonderimde unity ile terrain olusturmayı gorecegiz.

Bunun icin öncelikle yeni bir proje olusturuyoruz.Ardından 'Create' menusunden bir 'Terrain' objesi olusturuyoruz.

Olusturdugumuz terrain'in yukseltilerini daha iyi görebilmemiz icin default olarak bulunan bir texture'u terrainime atıyorum.

 

-->Ardından terrainin yükseligi default olarak 600 ayarlanmıs bu benim icin cok fazla  bu yuzden 'Terrain Height' ayarını 200 yapıyorum.

 

 

 

⇒ Artık kodlarımızı yazmaya hazırız.

 private void Start()
        {
           TerrainData terrainData = gameObject.GetComponent().terrainData;
        
           terrainData.SetHeights(0, 0, GenerateTerrain(depth1,depth2,terrainData));
        }

⇒'Generate_Terrain' isimli bir script olusturdum ve 'Terrain' objesine verdim. 

-->terrainData degiskenine 'terrainData' yı ekliyoruz.
-->Bu sayade terrain ile ilgili degistirmek istedigimiz verilere erisebilecegiz.
-->Ardından 'SetHeights' methoduyla (0,0) noktasından baslayarak butun noktaların yuksekligini belirliyoruz.

 

private float[,] GenerateTerrain(float depth1,float depth2,TerrainData data)
        {
            float[,] heights = new float[data.heightmapWidth, data.heightmapHeight];

            float height1 = 0;
            float height2 = 0;

            for(int x = 0; x < data.heightmapWidth; x++)
            {
            for(int z = 0; z < data.heightmapHeight; z++)
               {
                  height1 = Mathf.PerlinNoise(x / depth1, z / depth1);
                  height2 = Mathf.PerlinNoise(x / depth2, z / depth2);

                  heights[x, z] = height1 + height2;
               }
            }

            return heights;
        }       

⇒ Burada önemli olan 'PerlinNoise' methodu, bu method bir gradyan goruntusu elde etmemizi saglar.
Yeryuzu sekillerinin ani bir yukselis yada inis yasamadan daha egimli bir halde olmasını saglar.

⇒Ve kodun son Hali!!!

using System.Collections;
        using System.Collections.Generic;
        using UnityEngine;

        public class Terrain_Generated : MonoBehaviour
        {
            public float depth1,depth2;

        private void Start()
        {
            TerrainData terrainData = gameObject.GetComponent().terrainData;
        
            terrainData.SetHeights(0, 0, GenerateTerrain(depth1,depth2,terrainData));
        }

        private float[,] GenerateTerrain(float depth1,float depth2,TerrainData data)
        {
            float[,] heights = new float[data.heightmapWidth, data.heightmapHeight];

            float height1 = 0;
            float height2 = 0;

            for(int x = 0; x < data.heightmapWidth; x++)
            {
                for(int z = 0; z < data.heightmapHeight; z++)
                {
                    height1 = Mathf.PerlinNoise(x / depth1, z / depth1);
                    height2 = Mathf.PerlinNoise(x / depth2, z / depth2);

                    heights[x, z] = height1 + height2;
                }
            }

            return heights;
        }
        }

 

⇒depth1 ve depth2 icin verdigim degerler.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

⇒Ve Sonuc

⇒Bir sonraki gonderide gorusmek uzere...